package com.ali.gulimall.member.controller;

import java.lang.reflect.Member;
import java.util.Arrays;
import java.util.Map;


import com.ali.common.Exception.BizCodeEnum;
import com.ali.gulimall.member.exception.PhoneException;
import com.ali.gulimall.member.exception.UsernameException;
import com.ali.gulimall.member.feign.CouponFeignService;
import com.ali.gulimall.member.vo.MemberRegisterVo;
import com.ali.gulimall.member.vo.SocialUser;
import com.ali.gulimall.member.vo.UserLoginVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.ali.gulimall.member.entity.MemberEntity;
import com.ali.gulimall.member.service.MemberService;
import com.ali.common.utils.PageUtils;
import com.ali.common.utils.R;



/**
 * 会员
 *
 * @author ali
 * @email 2916227265@qq.com
 * @date 2022-07-15 15:57:04
 */
@RestController
@RequestMapping("member/member")
public class MemberController {
    @Autowired
    private MemberService memberService;

    @Autowired
    private CouponFeignService couponFeignService;

    @RequestMapping("/coupons")
    public R test(){
        MemberEntity memberEntity = new MemberEntity();
        memberEntity.setNickname("张三");
        R r = couponFeignService.membercoupons();
        return R.ok().put("member",memberEntity).put("coupons",r.get("coupons"));

    }

    //会员注册
    @PostMapping("/regist")
    public R regist(@RequestBody MemberRegisterVo vo){
        try {
            memberService.regist(vo);
        }catch (PhoneException e){
            return R.error(BizCodeEnum.PHONE_EXIST_EXCEPTION.getCode(), BizCodeEnum.PHONE_EXIST_EXCEPTION.getMessage());

        }catch (UsernameException e){
            return R.error(BizCodeEnum.USER_EXIST_EXCEPTION.getCode(), BizCodeEnum.USER_EXIST_EXCEPTION.getMessage());
        }
        return R.ok();
    }


    @PostMapping(value = "/login")
    public R login(@RequestBody UserLoginVo vo){
        MemberEntity memberEntity = memberService.login(vo);
        if (memberEntity != null) {
            return R.ok().put("data",memberEntity);
        } else {
            return R.error(BizCodeEnum.LOGINACCT_PASSWORD_EXCEPTION.getCode(),BizCodeEnum.LOGINACCT_PASSWORD_EXCEPTION.getMessage());
        }

    }
    @PostMapping(value = "/oauth2/login")
    public R oauthLogin(@RequestBody SocialUser socialUservo) throws Exception {
        MemberEntity memberEntity = memberService.login(socialUservo);
        if (memberEntity != null) {
            return R.ok().put("data",memberEntity);
        } else {
            return R.error(BizCodeEnum.LOGINACCT_PASSWORD_EXCEPTION.getCode(),BizCodeEnum.LOGINACCT_PASSWORD_EXCEPTION.getMessage());
        }

    }




    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = memberService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Long id){
		MemberEntity member = memberService.getById(id);

        return R.ok().put("member", member);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody MemberEntity member){
		memberService.save(member);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody MemberEntity member){
		memberService.updateById(member);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] ids){
		memberService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
